//
//  GLLeftMenuController.m
//  GLSliderView
//
//  Created by 张国梁 on 15/11/28.
//  Copyright © 2015年 CoderGL. All rights reserved.
//

#import "GLLeftMenuController.h"
#import "GLItem.h"

@interface GLLeftMenuController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *groups;

@end

@implementation GLLeftMenuController

- (NSMutableArray *)groups {
    if (!_groups) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

#pragma mark -  初始化操作
- (instancetype)init {
    if (self = [super init]) {
        // 添加数据
        [self setupData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 添加表视图
    [self setupTableView];
}

#pragma mark - 设置数据
- (void)setupData {
    
    NSMutableArray *items = [NSMutableArray arrayWithCapacity:10];
    UIViewController *interfaceVc = [UIViewController new];
    [items addObject:[self setupItemWithTitle:@"接口" destVc:interfaceVc]];
    
    UIViewController *authorVc = [UIViewController new];
    [items addObject:[self setupItemWithTitle:@"授权"destVc:authorVc]];
    
    UIViewController *weixinVc = [UIViewController new];
    [items addObject:[self setupItemWithTitle:@"微信" destVc:weixinVc]];
    
    UIViewController *qqVc = [UIViewController new];
    [items addObject:[self setupItemWithTitle:@"QQ" destVc:qqVc]];
    
    UIViewController *weiboVc = [UIViewController new];
    [items addObject:[self setupItemWithTitle:@"新浪微博" destVc:weiboVc]];
    
    UIViewController *showVc = [UIViewController new];
    [items addObject:[self setupItemWithTitle:@"演示" destVc:showVc]];
    
    UIViewController *shareVc = [UIViewController new];
    [items addObject:[self setupItemWithTitle:@"自定义分享界面" destVc:shareVc]];
    
    UIViewController *userInfoVc = [UIViewController new];
    [items addObject:[self setupItemWithTitle:@"获取用户信息" destVc:userInfoVc]];
    
    
    UIViewController *authorInfoVc = [UIViewController new];
    [items addObject:[self setupItemWithTitle:@"获取授权信息" destVc:authorInfoVc]];
    
    UIViewController *apiVc = [UIViewController new];
    [items addObject:[self setupItemWithTitle:@"调用API" destVc:apiVc]];
    
    NSMutableArray *moreItems = [NSMutableArray array];
    
    [moreItems addObject:[self setupItemWithTitle:@"关注新浪微博" destVc:nil]];
    [moreItems addObject:[self setupItemWithTitle:@"关注腾讯微博" destVc:nil]];
    
    
    [self.groups addObject:items];
    [self.groups addObject:moreItems];
    
}

- (GLItem *)setupItemWithTitle:(NSString *)title destVc:(UIViewController *)destVc {
    GLItem *item = [GLItem new];
    item.title = title;
    item.destVc = destVc;
    item.destVc.title = title;
    return item;
}


- (void)setupTableView {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(X_OFF_SET, Y_OFF_SET, 0, 0) style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView = tableView;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.groups[section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *const ID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    GLItem *item = self.groups[indexPath.section][indexPath.row];
    
    cell.textLabel.text = item.title;
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return @"更多";
    }
    return nil;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    GLItem *item = self.groups[indexPath.section][indexPath.row];
    
    if ([self.delegate respondsToSelector:@selector(leftMenu:didClick:)]) {
        [self.delegate leftMenu:self didClick:item];
    }
}


- (void)setDelegate:(id<GLLeftMenuControllerDelegate>)delegate {
    _delegate = delegate;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    
}

@end
